'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 删除好友
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../../consts/code');
var PlayerManager = require('../../../../libs/playerManager');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var friend_role_id = msg.friend_role_id;
    if (!friend_role_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var my_player_manager = new PlayerManager(role_id);
    var other_player_manager = new PlayerManager(friend_role_id);
    async.waterfall(
        [
            function (cb) {
                my_player_manager.refreshData(cb);
            },
            function (cb) {
                my_player_manager.getModel(
                    ['friend', 'role', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                other_player_manager.getModel(
                    ['friend', 'role', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var my_friend_model = my_player_manager.friend;
                var other_friend_model = other_player_manager.friend;

                // 删除好友
                my_friend_model.removeFriend(friend_role_id);
                other_friend_model.removeFriend(role_id);
                my_player_manager.save();
                other_player_manager.save();
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
